﻿using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Events.Bus.Entities;
using Abp.Events.Bus.Handlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FwProject.Core.SettlementInfoModel;
using FwProject.Core.Distribution;
using Abp.Runtime.Caching;

using FwProject.Restaurant;
using FwProject.Users;
using Abp.RemoteEventBus;
using Abp.ObjectMapping;
using FwProject.Distribution.Dto;
using FwProject.Restaurant.Dto;
using FwProject.PublicService.JPush;
using FwProject.BackGround;
using FwProject.BackGround.DistributionEnter;
using Abp.BackgroundJobs;
using FwProject.Comm;
using Abp.Logging;
using Castle.Core.Logging;
using Abp;
using Abp.Domain.Uow;

namespace FwProject.Application.EventHandl
{
    public class DistributionInfoHandle : IEventHandler<EntityChangedEventData<DistributionInfo>>, IEventHandler<EntityCreatedEventData<DistributionInfo>>,
    ITransientDependency
    {
        public readonly ICacheManager _catchmanager;
        public readonly IJPushAppservice _jPushAppservice;
        public readonly IRestaurantUserAppService _restaurantUserAppService;
        public readonly IRestaurantAppService _restaurantAppService;
        public readonly IUserAppService _userAppService;
        private readonly IBackgroundJobManager _backgroundJobManager;

        private readonly IRepository<DistributionInfo, Guid> _repositoryDistributionInfo;
        private readonly IRepository<SettlementInfo, Guid> _repositorySettlementInfo;
        public ILogger _logger { get; set; }

        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }//缓存对象
        public IRemoteEventBus remoteEventBus { get; set; }//缓存对象
        public DistributionInfoHandle(ICacheManager catchmanager,IJPushAppservice jPushAppservice,
                                      IRestaurantUserAppService restaurantUserAppService,
                                      IRestaurantAppService restaurantAppService,
                                      IUserAppService userAppService,
                                      IBackgroundJobManager backgroundJobManager, IRepository<DistributionInfo, Guid> repositoryDistributionInfo,
                                      IRepository<SettlementInfo, Guid> repositorySettlementInfo
            )
        {
            _guid = SequentialGuidGenerator.Instance;
            _repositorySettlementInfo = repositorySettlementInfo;
            _repositoryDistributionInfo = repositoryDistributionInfo;
            _logger = NullLogger.Instance;
            _backgroundJobManager = backgroundJobManager;
            _catchmanager = catchmanager;
            _jPushAppservice=jPushAppservice;
            _restaurantUserAppService=restaurantUserAppService;
            _restaurantAppService =restaurantAppService;
            _userAppService = userAppService;
            _map = NullObjectMapper.Instance;
            remoteEventBus = NullRemoteEventBus.Instance;
        }
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="eventData">Event data.</param>
        [UnitOfWork]
        public void HandleEvent(EntityChangedEventData<DistributionInfo> eventData)
        {
        

            if (eventData.Entity.SettlementId.HasValue==false && eventData.Entity.Pricestatus!=Pricestatus.已结账) {
                ///根据状态通知司机获取餐厅配送情况
                var eventDate = new RemoteEventData(RemoteTypeHelp.TypeDistributionType)
                {
                    Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id }
                };
                remoteEventBus.Publish(RemoteTypeHelp.TopicDistribution, eventDate);
     
            }
            

        }
        [UnitOfWork]
        public void HandleEvent(EntityCreatedEventData<DistributionInfo> eventData)
        {

            ///7天后自动收货 感觉有点鸡肋
            _logger.Info($"{eventData.Entity.Id}配送成功");
            //_backgroundJobManager.Enqueue<DistributionEnterBackGround, BasArg>(new BasArg
            // {
            //    Id = eventData.Entity.Id
            // }, BackgroundJobPriority.Normal,TimeSpan.FromDays(7.0));
            ///后台生成当前日结的账单
            var remoteEventData = new RemoteEventData(RemoteTypeHelp.TYPEDistributionBySettlementInfo)
            {
                Data = { [RemoteTypeHelp.MODELID] = eventData.Entity.Id }
            };
            remoteEventBus.Publish(RemoteTypeHelp.TopicSettlementInfoDistribution, remoteEventData);

        }


    }
}
